package com.adwin.addcoin.ui.task

import android.app.Application
import android.text.TextUtils
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.adwin.addcoin.repository.SchedulerTransformer
import com.adwin.addcoin.repository.api.TestApi
import com.adwin.addcoin.repository.bean.TaskDetailBean
import com.adwin.addcoin.repository.net.RetrofitClient
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import org.json.JSONObject

class TaskDetailModel (application: Application):AndroidViewModel(application){
    var taskDetailBean = MutableLiveData<TaskDetailBean>()
    var isChangTask = MutableLiveData(false)
    fun getTaskDetail(orderId:String){
        RetrofitClient.getInstance().getService(TestApi::class.java)
            .getTaskDetail(orderId.toLong()).compose(
                SchedulerTransformer()
            ).subscribe({
                if (it.isSuccessful){
                    val base = it.body()
                    if (base?.code == 200){
                        base?.data?.let {taskDetail ->
                            taskDetailBean.value = taskDetail
                        }
                    }
                }
            },{

            })
    }

    fun getActivityDetail(activityId:Long){
        RetrofitClient.getInstance().getService(TestApi::class.java)
            .getActivityDetail(activityId).compose(
            SchedulerTransformer()
        ).subscribe({
                if (it.isSuccessful){
                    val base = it.body()
                    if (base?.code == 200){
                        base?.data?.let {taskDetail ->
                            taskDetailBean.value = taskDetail
                        }
                    }
                }
        },{

        })
    }

    fun sendTask(url:String){
//        if(url.indexOf("https://") != -1){
//
//        }else if (url.indexOf("http://") != -1){
//            httpNet(url.replace("http://",""))
//        }

    }

    private fun httpNet(url: String){
        RetrofitClient.getInstance().getService(TestApi::class.java)
            .senTask(url).compose(SchedulerTransformer()).subscribe({

            },{

            })
    }

    fun startTask(offerId:String = "",activityId:Long){
        val json = JSONObject()
        if (activityId>0){
            json.put("activityId",activityId)
            json.put("orderId",offerId.toLong())
        }else{
            json.put("orderId",offerId.toLong())
        }
        val requestBody: RequestBody = MultipartBody.create(MediaType.get("application/json;charset=utf-8"), json.toString())
        RetrofitClient.getInstance().getService(TestApi::class.java).startTask(requestBody).compose(
            SchedulerTransformer()
        ).subscribe({
            if (it.isSuccessful&&it.body()?.code == 200){
                if (it.body()?.data == true){
                    isChangTask.value = true
                }
            }
        },{

        })
    }
}